package com.ruoyi.service.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.YesNoEnum;
import com.ruoyi.service.project.entity.LaborFavouriteEntity;
import com.ruoyi.service.project.mapper.LaborFavouriteMapper;
import com.ruoyi.service.project.service.LaborFavouriteService;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户项目收藏表 服务实现类
 * </p>
 *
 * @author Zane
 * @since 2025-10-23
 */
@Service
public class LaborFavouriteServiceImpl extends
    ServiceImpl<LaborFavouriteMapper, LaborFavouriteEntity>
    implements LaborFavouriteService {

  @Override
  public List<LaborFavouriteEntity> getByUserId(Long userId) {
    final LambdaQueryWrapper<LaborFavouriteEntity> query = effectQuery()
        .orderByDesc(LaborFavouriteEntity::getId)
        .eq(LaborFavouriteEntity::getLaborId, userId);
    return list(query);
  }

  @Override
  public LaborFavouriteEntity getByUserIdAndProjectId(Long userId, String projectId) {
    final LambdaQueryWrapper<LaborFavouriteEntity> query =
        new LambdaQueryWrapper<LaborFavouriteEntity>()
            .eq(LaborFavouriteEntity::getLaborId, userId)
            .eq(LaborFavouriteEntity::getProjectId, projectId);
    return getOne(query);
  }

  @Override
  public Long getCollectCount(Long userId) {
    return count(effectQuery().eq(LaborFavouriteEntity::getLaborId, userId));
  }

  @Override
  public boolean checkIsCollect(Long userId, String projectId) {
    return count(effectQuery().eq(LaborFavouriteEntity::getLaborId, userId)
        .eq(LaborFavouriteEntity::getProjectId, projectId)) > 0;
  }

  private LambdaQueryWrapper<LaborFavouriteEntity> effectQuery() {
    return new LambdaQueryWrapper<LaborFavouriteEntity>()
        .eq(LaborFavouriteEntity::getDeleteFlag, YesNoEnum.NO.getCode());
  }
}
